﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using TaggerApp.ORD;
using System.Data;
using System.Configuration;
using TaggerApp.Authentication;

namespace TaggerApp.Admin.Pages.Settings
{
    public partial class Default : System.Web.UI.Page
    {
        public string appName, apiKey;
        protected void Page_Load(object sender, EventArgs e)
        {
            //User myUser = (User)Session["user"];
            //if (myUser == null)
            //{
            //    Response.Redirect("~/Pages/Login");
            //}
            //else if (!myUser.Role.Equals("Admin"))
            //{
            //    Response.Redirect("~/Pages/Unauthorized/Unauthorized.aspx");
            //}

            Global title = new Global();
            title.GetTitleFromDB();
            Page.Title = "Admin Settings - " + title.appName;
            lblCurrentKey.Visible = false;
            btnHideKey.Visible = false;
            //btnHideAppName.Visible = false;
        }

        #region Save Button click method
        protected void btnSaveKey_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("../../../Web.config");

            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(path);

            XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings");
            XmlNodeList nodeAppSettings = nodeList[0].ChildNodes;
            XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes;
            xmlAttCollection[1].InnerXml = txtBoxAPIKey.Text;
            xDoc.Save(path);

            //Saving a textbox value to tblAdminConfig

            using (mstaggerEntities entities = new mstaggerEntities())
            {
                //Insert a Record
                //===================================
                tblAdminConfig admin = new tblAdminConfig();
                //fill the object with data
                admin.applicationName = txtBoxApplicationName.Text;
                admin.APIKey = txtBoxAPIKey.Text;
                //admin.applicationLogoURL = txtBoxLogoURL.Text;
                //Call the method to add the object to the table
                entities.tblAdminConfigs.Add(admin);
                //Save the changes to the DB
                entities.SaveChanges();
            }
        }
        #endregion

        protected void btnViewKey_Click(object sender, EventArgs e)
        {
            apiKey = ConfigurationManager.AppSettings["MSTagAPIKey"];
            lblCurrentKey.Text = apiKey;
            lblCurrentKey.Visible = true;
            btnViewKey.Visible = false;
            btnHideKey.Visible = true;
            //if (btnViewAppName.Visible == true)
            //{
            //    btnHideAppName.Visible = false;
            //}
            //else { btnHideAppName.Visible = true; lblShowAppName.Visible = true; }
        }

        protected void btnHideKey_Click(object sender, EventArgs e)
        {
            lblCurrentKey.Visible = false;
            btnHideKey.Visible = false;
            btnViewKey.Visible = true;
            //if (btnViewAppName.Visible == true)
            //{
            //    btnHideAppName.Visible = false;
            //}
            //else { btnHideAppName.Visible = true; lblShowAppName.Visible = true; }
        }

        //protected void btnViewAppName_Click(object sender, EventArgs e)
        //{
        //    Global name = new Global();
        //    lblShowAppName.Text = name.appName;
        //    lblShowAppName.Visible = true;
        //    btnViewAppName.Visible = false;
        //    btnHideAppName.Visible = true;
        //    if (btnViewKey.Visible == true)
        //    {
        //        btnHideKey.Visible = false;
        //    }
        //    else { btnHideKey.Visible = true; lblCurrentKey.Visible = true; }
        //}

        //protected void btnHideAppName_Click(object sender, EventArgs e)
        //{
        //    lblShowAppName.Visible = false;
        //    btnViewAppName.Visible = true;
        //    btnHideAppName.Visible = false;
        //    if (btnViewKey.Visible == true)
        //    {
        //        btnHideKey.Visible = false;
        //    }
        //    else { btnHideKey.Visible = true; lblCurrentKey.Visible = true; }
        //}

    }
}